package meihua.stopcarapp.model;

import java.util.List;

import meihua.mylibrary.basebean.ResultModel;
import meihua.mylibrary.baserx.RxSchedulers;
import meihua.stopcarapp.api.Api;
import meihua.stopcarapp.api.URLType;
import meihua.stopcarapp.bean.ParkingEntity;
import meihua.stopcarapp.bean.result.LadvanceResult;
import meihua.stopcarapp.bean.result.LockResult;
import meihua.stopcarapp.bean.result.UnLadvanceResult;
import meihua.stopcarapp.bean.result.UserResult;
import meihua.stopcarapp.presenter.contract.ParkingContract;
import rx.Observable;
import rx.functions.Func1;

/**
 * Name: LGZ
 * Time: 2017/5/6.
 * Dec:
 */

public class ParkingModel implements ParkingContract.Model {

    @Override
    public Observable<UserResult> getUserById(String uid, String phone) {
        return Api.getDefault(URLType.STOPCAR_URL)
                .getUserById(uid, phone)
                .map(new Func1<ResultModel<UserResult>, UserResult>() {
                    @Override
                    public UserResult call(ResultModel<UserResult> userEntityResultModel) {
                        return userEntityResultModel.getData();
                    }
                })
                .compose(RxSchedulers.<UserResult>io_main());
    }

    @Override
    public Observable<List<ParkingEntity>> getParkingList(String longitude, String latitude) {
        return Api.getDefault(URLType.STOPCAR_URL)
                .getParkingList(longitude, latitude)
                .map(new Func1<ResultModel<List<ParkingEntity>>, List<ParkingEntity>>() {
                    @Override
                    public List<ParkingEntity> call(ResultModel<List<ParkingEntity>> listResultModel) {
                        return listResultModel.getData();
                    }
                })
                .compose(RxSchedulers.<List<ParkingEntity>>io_main());
    }

    @Override
    public Observable<LadvanceResult> secdLadvance(String phone, String parking_list_num) {
        return Api.getDefault(URLType.STOPCAR_URL)
                .sendLadvance(phone, parking_list_num)
                .map(new Func1<ResultModel<LadvanceResult>, LadvanceResult>() {
                    @Override
                    public LadvanceResult call(ResultModel<LadvanceResult> listResultModel) {
                        return listResultModel.getData();
                    }
                })
                .compose(RxSchedulers.<LadvanceResult>io_main());
    }

    @Override
    public Observable<UnLadvanceResult> secdUnLadvance(String adv_id, String phone, String parking_list_num) {
        return Api.getDefault(URLType.STOPCAR_URL)
                .sendUnLadvance(adv_id, phone, parking_list_num)
                .map(new Func1<ResultModel<UnLadvanceResult>, UnLadvanceResult>() {
                    @Override
                    public UnLadvanceResult call(ResultModel<UnLadvanceResult> listResultModel) {
                        return listResultModel.getData();
                    }
                })
                .compose(RxSchedulers.<UnLadvanceResult>io_main());
    }

    @Override
    public Observable<LockResult> secdOpenLock(String phone, String parking_list_num) {
        return Api.getDefault(URLType.STOPCAR_URL)
                .sendOpenLock( phone, parking_list_num)
                .map(new Func1<ResultModel<LockResult>, LockResult>() {
                    @Override
                    public LockResult call(ResultModel<LockResult> listResultModel) {
                        return listResultModel.getData();
                    }
                })
                .compose(RxSchedulers.<LockResult>io_main());
    }

}
